/**
 * Created by zycool on 2016/8/17.
 * 附属物 - 管道在线数据 - 基础数据
 */


;(function ($, window, document, undefined) {

    // 管道
    var $pipeline = $('#JQ_pipeline').select3();
    // 管段
    var $segment = $('#JQ_segment').select3({
        title: '管段'
    });
    // 起点位置
    var $begin = $('#JQ_begin').select3({
        title: '起点位置',
        server: true,
        mileType: 'float',
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment}
    });
    // 终点位置
    var $end = $('#JQ_end').select3({
        title: '终点位置',
        server: true,
        mileType: 'float',
        param: {'pipeLineId': $pipeline, 'pipeSegmentId': $segment, 'beginSignId': $begin}
    });
    // 触发管道
    $pipeline.element.on('select', function (e, v) {
        var id = v || $pipeline.val();
        if (id) {
            // 管段
            DyyBase.ajax(DyyBase.url_segment(), {pipeLineId: id}, function (dataset) {
                var json = DyyBase.getSelect(dataset);
                $segment.add(json);
            });
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_pipe_pos(), {pipeLineId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin', true);
                var end = DyyBase.getPos(dataset, 'end', true);
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $segment.add([]);
            $begin.add([]);
            $end.add([]);
        }
    });
    // 触发管段
    $segment.element.on('select', function (e, v) {
        var id = v || $segment.val();
        if (id) {
            // 起点 & 终点
            DyyBase.ajax(DyyBase.url_segment_pos(), {pipeSegmentId: id}, function (dataset) {
                var begin = DyyBase.getPos(dataset, 'begin', true);
                var end = DyyBase.getPos(dataset, 'end', true);
                $begin.asset(begin);
                $end.asset(end);
            });
        } else {
            $begin.add([]);
            $end.add([]);
        }
    });
    // 附属物状态
    var $adjunct_status = $('#JQ_adjunct_status').select3({
        search: false
    });
    DyyBase.ajax(baseDataApi.url_adjunct_status(),{},function(dataset){
        var json = DyyBase.getSelect(dataset.result);
        json.shift();
        $adjunct_status.add(json);
    });
    // 表格
    var $table_adjunct = $('#JQ_table_adjunct');
    // 导出
    var $export_adjunct = $('#JQ_export_adjunct');
    // 列表切换按钮
    var $list_span = $('#JQ_toggle_list_span');
    // 地图切换按钮
    var $map_span = $('#JQ_toggle_map_span');

    // 条件对象
    var filter = {
        getSearch: function (param) {
            var data = {};
            // 分页参数
            if (param) {
                if (param.start == 0) {
                    data['jsonBean.pageNum'] = 1;
                } else {
                    data['jsonBean.pageNum'] = param.start / 10 + 1;
                }
                data['jsonBean.pageCount'] = param.length;
            }
            // 管道
            data.pipeLineId = function () {
                var v = $pipeline.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            data.pipeLineName = function () {
                var v = $pipeline.val('text');
                if (v) {
                    return v;
                }
                return '';
            }();
            // 起点
            data.startMileage = function () {
                var v = $begin.val('mile');
                if (DyyBase.notEmpty(v)) {
                    return v;
                }
                return '';
            }();
            // 终点
            data.endMileage = function () {
                var v = $end.val('mile');
                if (DyyBase.notEmpty(v)) {
                    return v;
                }
                return '';
            }();
            // 附属物状态
            data.appurtenanceStatus = function () {
                var v = $adjunct_status.val();
                if (v) {
                    return v;
                }
                return '';
            }();
            return data;
        },
        getExport: function () {
            return filter.getSearch();
        }
    };

    // 初始加载表格
    var DTADJUN = $table_adjunct.DataTable({
        'language': {
            'paginate': {
                'next': '下一页',
                'previous': '上一页'
            },
            'info': '本次检索共 _TOTAL_ 条，每页显示 10 条，共 _PAGES_ 页',
            'zeroRecords': DyyBase.table_empty(),
            'infoEmpty': ''
        },
        'autoWidth': false,
        'order': [],
        'serverSide': true,
        'pageLength': 10,
        'ordering': false,
        'processing': false,
        'searching': false,
        'lengthChange': false,
        'ajax': function (data, callback) {
            var param = filter.getSearch(data);
            DyyBase.ajax(baseDataApi.url_adjunct_search(), param, function (dataset) {
                var returnData = {};
                returnData.draw = data.draw;
                returnData.recordsTotal = dataset.result.total;
                returnData.recordsFiltered = dataset.result.total;
                returnData.data = DyyBase.clearNull(dataset.result.list);
                callback(returnData);
            });
        },
        'drawCallback': function () {
            var th = $table_adjunct.find('th');
            var empty = $table_adjunct.find('td.dataTables_empty');
            empty.attr('colspan',th.length);
            if($map_span.hasClass('active')){
                $map_span.removeClass('active');
                DyyBase.showMap('map', $table_adjunct, 'JQ_dyMap', [{name: 'gisId', type: 'adjunct', mode: 'adjunct'}]);
            }
        },
        'columns': [
            {
                data: 'pipeLineName', render: function (data, type, row) {
                return '<span title="' + data + '">' + data + '</span><input type="hidden" name="gisId" data-mode="'+row.mergeType+'" value="' + row.gisId + '">';
            }
            },
            {
                data: 'appurtenanceType', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'fromReference', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'appurtenanceStatus', render: function (data) {
                return '<span title="' + data + '">' + data + '</span>';
            }
            },
            {
                data: 'id', render: function (data, type, row) {
                return '<i title="详情" class="zy-details-icon" data-type="' + row.mergeType + '" data-id="' + data + '"></i>';
            }
            }
        ]
    });

    // 详情
    $table_adjunct.on('click', 'i.zy-details-icon', function () {
        var $this = $(this);
        var type = $this.attr('data-type');
        var id = $this.attr('data-id');
        if(type === '0'){
            DyyBase.showDetails($this,DTADJUN,$table_adjunct,pipeTemplate['adjunct0']);
        }else if(type === '1'){
            DyyBase.showDetails($this,DTADJUN,$table_adjunct,pipeTemplate['adjunct1']);
        }
    });

    // 搜索
    $('#JQ_search_adjunct').click(function () {
        if (DTADJUN) {
            DTADJUN.draw();
        }
    });

    // 导出
    $export_adjunct.click(function () {
        var param = (filter.getExport instanceof Function) ? filter.getExport() : filter.getExport;
        $(this).EXPT({
            type: 'name',
            name: param.pipeLineName + '附属物信息表',
            param: param,
            server: baseDataApi.url_adjunct_export()
        });
    });

    // 显示地图
    $map_span.click(function () {
        $export_adjunct.hide();
        DyyBase.showMap('map', $table_adjunct, 'JQ_dyMap', [{name: 'gisId', type: 'adjunct', mode: 'adjunct'}]);
    });

    // 显示列表
    $list_span.click(function () {
        $export_adjunct.show();
        DyyBase.showMap('list', $table_adjunct, 'JQ_dyMap', [{name: 'gisId', type: 'adjunct', mode: 'adjunct'}]);
    });

})(jQuery, window, document);